var goodsid = window.location.search.substr(4);
var shop_tpye, area_id;
//ajax模板
function jshtml(data, scriptdom) {
	var mainmax = document.getElementById('detailmain');
	var html = '';
	var htmlm = scriptdom.innerHTML;
	var price = data.price;
	if(shop_tpye == 1) {
		if(data.goodsname.match(/绣球/)) {
			price += "元/只";
		} else {
			price += "元/扎";
		}
	}
	htmlm = htmlm.replace(/\{\{h3\}\}/, data.goodsname);
	htmlm = htmlm.replace(/\{\{price\}\}/, price);
	htmlm = htmlm.replace(/\{\{sc\}\}/, data.sc);
	html += htmlm;
	mainmax.innerHTML = html;
};
//图片展示模板
function photos(data) {
	var mainmax = document.getElementById('swipe');
	var html = '';
	for(var k in data) {
		//console.log(data[k]);
		var htmlm = '<div class="swiper-slide"><img src= ' + data[k] + ' /></div>';
		html += htmlm;
	}
	mainmax.innerHTML = html;
	var swiper = new Swiper('.swiper-container', {
		pagination: '.swiper-pagination',
		paginationClickable: true
	});
};
//END
//显示提示信息
function infoMsg(message) {
	var info = document.querySelector(".info");
	info.style.display = "block";
	var infoCon = info.querySelector("p");
	if(message) {
		infoCon.innerHTML = message;
	}
	info.style.opacity = 1;
	setTimeout(function() {
		info.style.opacity = 0;
	}, 1000);
	setTimeout(function() {
		info.style.display = "none";
	}, 1500)

}
//加入购物车
function addg() {
	var addgwc = document.getElementById('addgwc');
	var footer = document.querySelector('footer');
	var input = footer.querySelector('input');
	var i = footer.querySelector('i');
	var s = footer.querySelector('s');
	var footerx = document.getElementById('footerx');
	var drak = document.querySelector('.drak');
	var check = document.getElementById('check');
	//ajax点击收藏
	function tapsc() {
		var mainsc = document.getElementById('mainsc');
		var xx = 'star_off.png';
		var token = selectByKey("like_token");
		Tap(mainsc, function(e, dom) {
			if(token) {
				//$.ajax({
				//    url: "",
				//    type: "post",
				//    data: {},
				//    success: function (value) {
				//    }
				//});
				if(xx == 'star_off.png') {
					xx = 'star_on.png';
					dom.querySelector('img').setAttribute('class', 'bounceIn');
				} else {
					xx = 'star_off.png';
					dom.querySelector('img').removeAttribute('class', 'bounceIn');
				}
				dom.querySelector('img').src = '../statics/images/detail/' + xx;
			} else {
				mark.style.display = "block";
			}

		});
	};
	tapsc();
	//收藏END
	//点击购物车时tap事件
	Tap(addgwc, function() {
		drak.style.height = document.body.clientHeight + 'px';
		//console.log(document.body.clientHeight);
		drak.addEventListener("touchmove", function(e) {
			e.preventDefault();
		});
		footer.style.opacity = 1;
		footer.style.bottom = 0;
	});
	//关闭tap事件
	Tap(footerx, function() {
		drak.style.height = '0px';
		footer.style.opacity = 0;
		footer.style.bottom = '-125px';
	});
	//确定tap事件
	var mark = document.querySelector(".markT");
	Tap(check, function() {
		drak.style.height = '0px';
		footer.style.bottom = '-125px';
		var num = document.querySelector("footer input").value;
		var token = selectByKey("like_token");
		//判断从特价花入口还是普通入口
		console.log(shop_tpye)
		if(token) {
			//请求添加到购物车
			$.ajax({
				url: pathName + "/api/v1/member/cart",
				type: "POST",
				dataType: "json",
				headers: {
					"Accept": "application/json",
					"Authorization": "Bearer " + token
				},
				data: {
					"goodId": goodsid,
					"shop_total": num,
					"shop_type": shop_tpye,
					"region_id": area_id
				},
				success: function(value) {
					console.log(value);
					var num = document.querySelector("header i");
					num.innerHTML = "(" + value.cart_count + ")";
					fly();
				},
				error: function(res) {
					var response = JSON.parse(res.responseText);
					if(response.code == 20041) {
						infoMsg("特价花不能与普通花一起购买");
					}
				}
			})
		} else {
			mark.style.display = "block";
		}

	});
	var sure = mark.querySelector("#sureT");
	var cancel = mark.querySelector("#cancleT");
	Tap(sure, function() {
		//      window.location.href = "personal/enter.html";
		window.location.href = "cityPartner/chooseLogin.html"
	});
	Tap(cancel, function() {
		mark.style.display = "none";
	});
	//加减tap事件
	i.style.color = 'lightgrey';

	Tap(i, function() {
		if(input.value <= 1) return;
		input.value--;
		if(input.value <= 1) {
			i.style.color = 'lightgrey';
		};
	});
	Tap(s, function() {
		if(input.value >= 20 && shop_tpye == 1) {
			infoMsg("特价花限购20扎");
			return;
		}
		input.value++;
		if(input.value >= 2) {
			i.style.color = '';
		}
	});
	input.addEventListener("keyup", function() {
		if(input.value >= 20 && shop_tpye == 1) {
			input.value = 20;
			infoMsg("特价花限购20扎");
		}
	})
};
//END
//计算main高度
function mainHeight() {
	var mainmax = document.getElementById('out');
	var height = document.documentElement.clientHeight || document.body.clientHeight;
	var h = height - height * 0.03 - 100;
	mainmax.style.height = h + 'px';
}
//求购物车里数量
function getNum() {
	var num = document.getElementById("good_num");
	var token = selectByKey("like_token");
	if(token) {
		//请求购物车里数量
		$.ajax({
			url: pathName + "/api/v1/member/cartNum",
			type: "GET",
			headers: {
				"accept": "application",
				"Authorization": "Bearer " + token
			},
			success: function(data) {
				num.innerHTML = "(" + data.cartNum + ")";
			}
		})
	}
}
//订购须知
function toKnow() {
	var knowInfo = document.querySelector(".know");
	var know = document.getElementById("know");
	knowInfo.style.display = "block";
	setTimeout(function() {
		knowInfo.style.opacity = 1;
	}, 50)
	Tap(know, function() {
		knowInfo.style.display = "block";
		setTimeout(function() {
			knowInfo.style.opacity = 1;
		}, 50)
	});
	Tap(knowInfo, function() {
		knowInfo.style.opacity = 0;
		setTimeout(function() {
			knowInfo.style.display = "none";
		}, 500);
	});
}
//进入客服
function kf() {
	var connect = document.querySelector("#mbottom span:first-child");
	Tap(connect, function() {
		location.href = "service.html";
	});
}
//点击购物车
function shopping() {
	var go = document.querySelector("header a:last-child");
	var token = selectByKey("like_token");
	Tap(go, function() {
		if(token) {
			var hash = location.hash.substr(1);
			if(hash) {
				window.location.href = "trolley.html#special";
			} else {
				window.location.href = "trolley.html";
			}

		} else {
			var mark = document.querySelector(".markT");
			mark.style.display = "block";
			var sure = mark.querySelector("#sureT");
			var cancel = mark.querySelector("#cancleT");
			Tap(sure, function() {
				//      window.location.href = "personal/enter.html";
				window.location.href = "cityPartner/chooseLogin.html"
			});
			Tap(cancel, function() {
				mark.style.display = "none";
			});
		}
	});
}

function fly() {
	var butterfly = document.createElement("div");
	var img = document.createElement('img');
	var src = document.querySelector('.swiper-slide-active').children[0].src;
	img.src = src;
	butterfly.appendChild(img);
	var mbottom = document.getElementsByClassName("fly_content")[0];
	mbottom.style.display = 'block';
	butterfly.className = 'butterfly';
	mbottom.appendChild(butterfly);
	var W = document.documentElement.clientWidth / 2 - 40;
	var H = document.documentElement.clientHeight - 109;
	var BW = 30;
	var opa = 1;
	var y = 0;
	var timer = setInterval(function() {
		y += H / 50;
		if(BW >= 20) {
			BW -= 0.2;
			opa -= 0.01;
		}
		x = 3 * W * y * y / (2 * H * H) - W * y / (2 * H);
		butterfly.style.transform = "translate3d(" + x + "px, -" + y + "px, 0px)";
		butterfly.style.width = BW + 'px';
		butterfly.style.height = BW + 'px';
		butterfly.style.opacity = opa;
		if(y >= H) {
			clearInterval(timer);
			butterfly.style.display = 'none';
			infoMsg("加入成功");
		}
	}, 20)
}
window.onload = function() {
	addg();
	mainHeight();
	getNum();
	kf();
	shopping();
	var hash = location.hash.substr(1);
	if(location.search.indexOf('&') != -1) {
		shop_tpye = 1;
		area_id = location.search.split("&")[1].split("=")[1];
	} else {
		shop_tpye = 0;
	}

	//设置轮播图高度
	var width = document.body.clientWidth || document.documentElement.clientWidth;
	var height = width / 1.5;
	var con = document.querySelector(".swiper-container");
	con.style.height = height + "px";
	con.style.overflow = "hidden";
	localStorage.removeItem("searchName");
	var goodsid = window.location.search.split("&")[0].split('=')[1];
	//请求商品详情
	$.ajax({
		url: pathName + "/api/v1/good/" + goodsid,
		type: "GET",
		data: {
			"shop_type": shop_tpye
		},
		dataType: "json",
		success: function(obj) {
			console.log(obj);
			//var obj = JSON.parse(data);

			var mask = document.querySelector(".mask");
			mask.style.display = "none";
			var imgArr = obj.thumbimg.split(",");
			imgArr.pop();
			//console.log(imgArr)
			jshtml(obj, document.getElementById('mode'));
			var detail = document.querySelector(".mul");
			detail.innerHTML = obj.content;
			photos(imgArr);
			toKnow();
		}
	});
};